package com.example.kt_all.ui


import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.TimeUtils
import com.example.kt_all.R
import com.example.kt_all.databinding.ActivityOperationStatusBinding
import com.example.kt_common.base.BaseActivity
import com.example.kt_common.util.Constants
import com.example.kt_common.util.ToastUtil
import com.loper7.date_time_picker.DateTimeConfig
import com.loper7.date_time_picker.dialog.CardDatePickerDialog


@Route(path = Constants.PATH_OPERATIONG_STATUS)
class OperationStatusActivity : BaseActivity<ActivityOperationStatusBinding>() {
    override fun getLayoutID(): Int {
        return R.layout.activity_operation_status
    }

    override fun init() {

        mBind.icCondition.tvStartTime.setOnClickListener {
            CardDatePickerDialog.builder(this).setTitle("选择开始时间").showBackNow(false)
                .setDisplayType(
                    DateTimeConfig.YEAR,//显示年
                    DateTimeConfig.MONTH,//显示月
                    DateTimeConfig.DAY,//显示日
                    DateTimeConfig.HOUR,//显示时
                    DateTimeConfig.MIN,//显示分
                    DateTimeConfig.SECOND//显示秒
                ).setLabelText("年", "月", "日", "时", "分", "秒").setOnChoose { millisecond ->
                    mBind.icCondition.tvStartTime.text = TimeUtils.millis2String(millisecond)
                }.build().show()


        }
        mBind.icCondition.tvEndTime.setOnClickListener {
            CardDatePickerDialog.builder(this).setTitle("选择结束时间").showBackNow(false)
                .setDisplayType(
                    DateTimeConfig.YEAR,//显示年
                    DateTimeConfig.MONTH,//显示月
                    DateTimeConfig.DAY,//显示日
                    DateTimeConfig.HOUR,//显示时
                    DateTimeConfig.MIN,//显示分
                    DateTimeConfig.SECOND//显示秒
                ).setLabelText("年", "月", "日", "时", "分", "秒").setOnChoose { millisecond ->
                    mBind.icCondition.tvEndTime.text = TimeUtils.millis2String(millisecond)

                }.build().show()
        }
        mBind.icQuickTime.tvYesterday.setOnClickListener {
            val currentTimeMillis = System.currentTimeMillis()
            val l = getStartTime(1, currentTimeMillis)
            mBind.icCondition.tvStartTime.text = TimeUtils.millis2String(l)
            mBind.icCondition.tvEndTime.text = TimeUtils.millis2String(currentTimeMillis)
        }
        mBind.icQuickTime.tvThreeDays.setOnClickListener {
            val currentTimeMillis = System.currentTimeMillis()
            val l = getStartTime(3, currentTimeMillis)
            mBind.icCondition.tvStartTime.text = TimeUtils.millis2String(l)
            mBind.icCondition.tvEndTime.text = TimeUtils.millis2String(currentTimeMillis)
        }

        mBind.icQuickTime.tvSevenDays.setOnClickListener {
            val currentTimeMillis = System.currentTimeMillis()
            val l = getStartTime(7, currentTimeMillis)
            mBind.icCondition.tvStartTime.text = TimeUtils.millis2String(l)
            mBind.icCondition.tvEndTime.text = TimeUtils.millis2String(currentTimeMillis)
        }

        mBind.icQuickTime.tvTenDays.setOnClickListener {
            val currentTimeMillis = System.currentTimeMillis()
            val l = getStartTime(10, currentTimeMillis)
            mBind.icCondition.tvStartTime.text = TimeUtils.millis2String(l)
            mBind.icCondition.tvEndTime.text = TimeUtils.millis2String(currentTimeMillis)
        }
        mBind.icQuickTime.tvFifteenDays.setOnClickListener {
            val currentTimeMillis = System.currentTimeMillis()
            val l = getStartTime(15, currentTimeMillis)
            mBind.icCondition.tvStartTime.text = TimeUtils.millis2String(l)
            mBind.icCondition.tvEndTime.text = TimeUtils.millis2String(currentTimeMillis)
        }
        mBind.icQuickTime.tvTwentyDays.setOnClickListener {
            val currentTimeMillis = System.currentTimeMillis()
            val l = getStartTime(20, currentTimeMillis)
            mBind.icCondition.tvStartTime.text = TimeUtils.millis2String(l)
            mBind.icCondition.tvEndTime.text = TimeUtils.millis2String(currentTimeMillis)
        }
        mBind.icQuickTime.tvTwentyFiveDays.setOnClickListener {
            val currentTimeMillis = System.currentTimeMillis()
            val l = getStartTime(25, currentTimeMillis)
            mBind.icCondition.tvStartTime.text = TimeUtils.millis2String(l)
            mBind.icCondition.tvEndTime.text = TimeUtils.millis2String(currentTimeMillis)
        }
        mBind.icQuickTime.tvThirtyDays.setOnClickListener {
            val currentTimeMillis = System.currentTimeMillis()
            val l = getStartTime(30, currentTimeMillis)
            mBind.icCondition.tvStartTime.text = TimeUtils.millis2String(l)
            mBind.icCondition.tvEndTime.text = TimeUtils.millis2String(currentTimeMillis)
        }
        mBind.btnQuery.setOnClickListener {
            if (mBind.icCondition.tvStartTime.text.isEmpty()) {
                ToastUtil.showMsg("请选择开始时间")
                return@setOnClickListener
            }
            if (mBind.icCondition.tvEndTime.text.isEmpty()) {
                ToastUtil.showMsg("请选择结束时间")
                return@setOnClickListener
            }
            mBind.icCondition.tvSelectVehicle.text = "粤AA91528"
            ARouter.getInstance().build(Constants.PATH_OPERATIONG_LIST)
                .withString(Constants.carNum, "粤AA91528")//车牌号码
                .withString(
                    Constants.startTime, mBind.icCondition.tvStartTime.text.toString()
                )//开始时间
                .withString(Constants.endTime, mBind.icCondition.tvEndTime.text.toString())//结束时间
                .navigation()
        }
    }

    /**
     * @param day 相差多少天
     * @param currentTime 当前毫秒 时间
     * */
    private fun getStartTime(day: Int, currentTime: Long): Long {
        val l = currentTime - (day * 24 * 60 * 60 * 1000)
        return l
    }
}


